﻿import MainEcharts from '@/components/common/MainEcharts.vue'
export default {
  components: {
    MainEcharts
  },
  props: ['element'],
  computed: {
    getTableColumns: function () {
      let currentTableColumns = []
      if ((this.element.type === 'table') && this.element.tableColumn.length) {
        this.element.tableColumn.forEach(item => {
          if (item.isShow) {
            this.$set(currentTableColumns, currentTableColumns.length, item)
          }
        })
      }
      if (currentTableColumns.length) {
        return currentTableColumns.sort(this.orderByObjAttr('sortNum'))
      } else {
        return []
      }
    }
  },
  mounted () {
    this.$nextTick(() => {
      this.initElement()
    })
  },
  methods: {
    // 初始化元素
    initElement () {
      let echartsSeries = null // 存储配置时保存的series
      if (this.element.type === 'table') {
        this.element.tableData = []
      } else if (this.element.type === 'echarts_line' || this.element.type === 'echarts_bar') {
        this.element.dataOptions.xAxis.data = []
        this.element.dataOptions.legend.data = []
        echartsSeries = (this.element.dataOptions.series && this.element.dataOptions.series.length) ? JSON.parse(JSON.stringify(this.element.dataOptions.series)) : []
        this.element.dataOptions.series = []
        if (this.element.type === 'echarts_bar') { // 柱状图时
          this.element.dataOptions.yAxis.data = []
        }
      } else if (this.element.type === 'echarts_pie') { // 饼图
        this.element.dataOptions.legend.data = []
        if (this.element.dataOptions.series && this.element.dataOptions.series.length) {
          echartsSeries = (this.element.dataOptions.series[0].data && this.element.dataOptions.series[0].data.length) ? JSON.parse(JSON.stringify(this.element.dataOptions.series[0].data)) : []
          this.element.dataOptions.series[0].data = []
        }
      }
      if (this.element.dataSet) {
        this.excuteCurrentSet(echartsSeries)
      }
    },
    // 执行指定的数据集
    excuteCurrentSet (echartsSeries) {
      this.$post('/data_set/' + this.element.dataSet + '/excute', {
        limit: 10000,
        page: 1
      }).then(data => {
        let dataResult = data.data.result
        if (dataResult.code === 'success') {
          let dataList = dataResult.data.data.QueryDatas
          if (this.element.type === 'table') {
            this.element.tableData = dataList
          } else if (this.element.type === 'echarts_line' || this.element.type === 'echarts_bar') {
            if (this.element.dimension) { // 维度
              dataList.forEach(item => {
                this.element.dataOptions.xAxis.data.push(item[this.element.dimension])
                if (this.element.type === 'echarts_bar') { // 柱状图时
                  this.element.dataOptions.yAxis.data.push(item[this.element.dimension])
                }
              })
            }
            if (echartsSeries && echartsSeries.length) {
              echartsSeries.forEach(item => {
                item.data = []
                if (item.name) {
                  this.element.dataOptions.legend.data.push(item.name)
                  for (let i = 0; i < dataList.length; i++) {
                    item.data.push(dataList[i][item.name])
                  }
                }
                this.$set(this.element.dataOptions.series, this.element.dataOptions.series.length, item)
              })
            }
          } else if (this.element.type === 'echarts_pie') {
            if (this.element.dimension && this.element.measurement) {
              dataList.forEach(item => {
                // 维度的值在legend
                this.element.dataOptions.legend.data.push(item[this.element.dimension])
                let hadEqual = false
                for (let i = 0; i < echartsSeries.length; i++) {
                  // 量度的值才是对应的value
                  if (echartsSeries[i].name === item[this.element.dimension]) {
                    hadEqual = true
                    echartsSeries[i].value = item[this.element.measurement]
                    this.$set(this.element.dataOptions.series[0].data, this.element.dataOptions.series[0].data.length, echartsSeries[i])
                    break
                  }
                }
                if (!hadEqual) { // 配置的时候咩有该项时
                  let obj = {
                    value: item[this.element.measurement],
                    name: item[this.element.dimension]
                  }
                  this.$set(this.element.dataOptions.series[0].data, this.element.dataOptions.series[0].data.length, obj)
                }
              })
            }
          }
        }
      })
    },
    // 对象数组排序
    orderByObjAttr (key) {
      return function (val1, val2) {
        return val1[key] - val2[key]
      }
    }
  },
  watch: {
    'elemet.model': {
      handler: function (val) {
        console.log(val)
      }
    }
  }
}
